package com.typing.battle.database;

import java.io.*;
import java.util.*;

import com.typing.battle.value.FriendDTO;

import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.util.*;

public class FriendManager { 

  private static final String TAG                 = "DBManager";
    
  public static final String DATABASE_NAME       = "typingBattle.db";
  public static final String DATABASE_TABLE      = "BATTLE_FRIENDS";
  public static final int    DATABASE_VERSION    = 1;
  
  public static final String  KEY_EMAIL                   = "FRIEND_EMAIL";
  public static final String  KEY_PHONE_NO                   = "FRIEND_PHONE_NO";
  public static final String  KEY_NAME          	   = "FRIEND_NAME";
  public static final String  KEY_BLOCK_YN     			   = "BLOCK_YN";
  
  
  private final String DATABASE_CREATE     = 
      "create table "+ DATABASE_TABLE + " (" + KEY_EMAIL + " ," +
    		  KEY_PHONE_NO + " varchar ," +
    		  KEY_NAME + " varchar not null," +
    		  KEY_BLOCK_YN + " varchar " + ");";        					  

    private SQLHelper mDbHelper;
    private SQLiteDatabase mDb;
    private Cursor mCursor;
    private final Context  mContext;
    private ArrayList<FriendDTO> list = new ArrayList<FriendDTO>();
    public FriendManager(Context context) {
        mContext = context;
    }
    
    public static boolean isExistDB() {
        boolean result = false;
        File file = new File("/data/data/com.typing.battle/databases/"+DATABASE_NAME);
                
        if (file.exists()){
            result = true;
        } else {
            Log.d("DBAdapter", "DB does not exist!!!!!!");
        }
        return result;
    }
        
    public SQLiteDatabase open() {
//        mDbHelper = new SQLHelper(mContext);
		mDbHelper = new SQLHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION);
		mDbHelper.setDataBaseInfo(DATABASE_TABLE, DATABASE_CREATE);
        mDb = mDbHelper.getWritableDatabase();
        return mDb;
    }
    
    public void close() {
        mDbHelper.close();
    }
    public ArrayList<FriendDTO> getFriends(String sql,String param) {
    	try{
	    	mCursor = mDb.rawQuery( sql, null );
	    	if ( mCursor != null )
	    	{
	    		if ( mCursor.moveToFirst() )
	    		{
	    			String strRow = "--------------------------------\n";
	    			for(int i = 0 ; i < mCursor.getColumnCount() ; i++ )
	    			{
	    				strRow += mCursor.getColumnName(i) + " | ";
	    			}
	    			strRow += "\n";
	    			Log.d("","colums===>>"+ strRow );
	
	    			do
	    			{
	    				FriendDTO pdata = new FriendDTO();
	    				strRow = "";
	    				for(int i = 0 ; i < mCursor.getColumnCount() ; i++ )
	    				{
	    					pdata=setFriendData(mCursor.getColumnName(i),mCursor.getString(i),pdata);
	    					strRow += mCursor.getString(i) + " | ";
	    				}
	    				strRow += "\n";
	    				Log.d("","DB result===>>"+  strRow );
	    				list.add(pdata);
	    			} while ( mCursor.moveToNext() );
	    		}
	    	}
	    	mCursor.close(); 
		}catch ( SQLException e )
		{
			
			Log.e( "SQLException", e.getMessage() );
		}
		finally
		{
			
			mDb.close();
			Log.i( "SQLite", "Database Close ... OK" );
		}

    	return list;
    }
    public FriendDTO setFriendData(String key ,String val,FriendDTO data){    	
    	if(key.equals(KEY_EMAIL))
    		data.setFriendEmail(val);
    	else if(key.equals(KEY_PHONE_NO))
    		data.setFriendPhoneNo(val);
    	else if(key.equals(KEY_NAME))
    		data.setFriendName(val);
    	else if(key.equals(KEY_BLOCK_YN))
    		data.setBlockYn(val);    	
    	return data;
    }
    public long addFriend(FriendDTO data) {
        ContentValues v = new ContentValues();     
        v.put(KEY_EMAIL, data.getFriendEmail());
        v.put(KEY_PHONE_NO, data.getFriendPhoneNo());
        v.put(KEY_NAME, data.getFriendName());
        v.put(KEY_BLOCK_YN, data.getBlockYn());
       
        return mDb.insert(DATABASE_TABLE, null, v);
    }
    
    public long updateFriend(FriendDTO data) {    	
        ContentValues v = new ContentValues();      
        v.put(KEY_PHONE_NO, data.getFriendPhoneNo());
        v.put(KEY_NAME, data.getFriendName());
        v.put(KEY_BLOCK_YN, data.getBlockYn());        
        
        return mDb.update(DATABASE_TABLE , v , KEY_EMAIL+" = "+data.getFriendEmail() , null);
    }
    
    public boolean deleteFriend(FriendDTO appInfo) {
        return mDb.delete(DATABASE_TABLE, KEY_EMAIL+" = "+appInfo.getFriendEmail() , null) > 0; 
    }
    
    public Cursor fetchAllFriends(String where) {      
        String query = "SELECT * FROM " + DATABASE_TABLE + where + " ORDER BY " + KEY_EMAIL + " ASC";
        return mDb.rawQuery(query, null);
    }
}
